/*
 * send_2mbps.c
 * Created on November 30, 2005; 12:53 PM
 * by vramadurai
 */

/************ System Header Files ***************/
#include <stdio.h>
#include <stdlib.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "PhyDebug.h"
#include "PhyStruct.h"
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "tables.h"
#include "tx_functions.h"
#include "iqdma_3500.h"
#include "icrdefs_3500.h"
#include "thread_globals.h"

void
send_2mbps(
                struct HS_PhyTx11bStr  *HS_PhyTx11bPtrTh1,
                ePHYSTATES_TH  *ePhyState,
                hs_uint8_t *ucStateTable2Bit,
                hs_uint32_t *crctab,
                hs_uint16_t usLength,
                hs_uint8_t	*psPayloadByte
             )
{
	hs_int32_t input_toggle;
	hs_uint32_t crc;
	hs_int32_t  crc_count;
	hs_int32_t	siPayloadBitCount;
	hs_uint8_t	ucBytePayload;
	hs_int32_t	siPayloadByteCount;
	hs_uint8_t ucBit;
	hs_uint8_t ucInputBit;
	hs_uint8_t *ucInputBit_shared;
	hs_uint8_t ucScramState;

	hs_int32_t i;
	siPayloadBitCount = 0;
	siPayloadByteCount = 0;

	crc = 0xFFFFFFFFU;
	crc_count = 0;
	input_toggle = 0;

	ucInputBit_shared = HS_PhyTx11bPtrTh1->ucInputBit_shared_2_55;
	ucScramState = HS_PhyTx11bPtrTh1->ucScramState;

    while(1){
    	START_CLOCK
    	//printf("test send step1\n");
		// CRC
		if(siPayloadByteCount < usLength-4){
			ucBytePayload = psPayloadByte[siPayloadByteCount];
			crc ^= ucBytePayload;
			crc = crctab[crc & 0xf] ^ (crc >> 4);
			crc = crctab[crc & 0xf] ^ (crc >> 4);
		}
		else{
			if(crc_count == 0){
				crc = crc ^ 0xffffffffU;
			}
			ucBytePayload = (crc >> (8*crc_count)) & 0xFF;
			crc_count++;
		}

        siPayloadByteCount++;
        //printf("test send step2\n");
        for(i=0; i<4; i++){

	        ucBit = ucBytePayload & 0x3;
	        ucBytePayload >>= 2;
			ucInputBit   = ucStateTable2Bit[ucScramState]^ucBit;
			ucScramState >>= 2;
			ucScramState |= (ucInputBit << 5);
			ucInputBit_shared[input_toggle*4+i] = ucInputBit;
		}
        input_toggle ^= 1;

        while(tx_thread1to2_flag1 != 0){
        	SPIN(2);
        }
        //printf("test send step3\n");
        tx_thread1to2_flag1 = 1;

		siPayloadBitCount += 8;

        if(siPayloadBitCount == (usLength*8))
        {
        	PRINT_CYCLES("send_2mbps_compute");
        	*ePhyState  = PHY_STATE_SWITCH_OFF_TH;
           break;
        }/* if siStateCount */
    }
}


